<template>
  <div class="wrapper">
    <div class="login-div">
      <div class="login-text"><b>管理系统登录</b></div>
      <el-form :model="user" :rules="rules">
        <el-form-item prop="username">
          <el-input  size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input  size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
        </el-form-item>


        <el-form-item style="text-align: right; font-size: large" >
          <!--          <el-button type="warning" size=""  autocomplete="off" @click="$router.push('/register')">注册</el-button>-->
          <el-button type="primary"  autocomplete="off" @click="login" class="bt">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      user: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login(){
      request.post("/Administration/user/login", this.user).then(res => {
        if(res.status === 10000){
          this.$router.push("/")
          this.$message.success("登陆成功")
          localStorage.setItem("user",JSON.stringify(res.data)) //把用户信息存到浏览器 其中带有token数据
          let testlocalStorage=localStorage.getItem("user") ?JSON.parse(localStorage.getItem("user")) : {}
          console.log('保存的是',testlocalStorage)
        }else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right, #9ecef5 , #4088e6);
  overflow: hidden;
}
.login-div{
  margin: 200px auto;
  background-color: #fff;
  width: 400px;
  height: 300px;
  padding: 30px;
  border-radius: 10px;
}
.login-text{
  margin: 20px 0;
  text-align: center;
  font-size: 30px;
}
.bt{
  margin:auto 165px;
}
</style>
